﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using LLFM.CAT.Model;
using LLFM.CAT.Service;
using System.IO;
using System.Text.RegularExpressions;

public partial class Admin_Ajax_AddProductAjax : AdminPage
{
    String State = String.Empty;
    List<M_SeriesImages> mImgs = null;
    string strImagePath = RequestHelper.GetString("photoArray");
    int seriesId = RequestHelper.GetInt("SeriesId", 0);
    string name = RequestHelper.GetString("Name");
    string model = RequestHelper.GetString("Model");
    string bucketCapacity = RequestHelper.GetString("BucketCapacity");
    string workWeight = RequestHelper.GetString("WorkWeight");
    string engine = RequestHelper.GetString("Engine");
    string power = RequestHelper.GetString("Power");
    string applyTo = RequestHelper.GetString("ApplyTo");
    string shortDescription = RequestHelper.GetString("ShortDescription");
    string advantage = RequestHelper.GetString("Advantage");
    string pdfUrl = RequestHelper.GetString("pdfUrl");
    string flashUrl = RequestHelper.GetString("flashUrl");
    string description = RequestHelper.GetString("editorDes").Replace("\n", "").Replace("\r\n", "");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Common.RequestHelper.IsAjaxRequest)
        {
            string p = @"/Date\((\d+)\)/";
            MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
            Regex reg = new Regex(p);
            strImagePath = reg.Replace(strImagePath, matchEvaluator);

            mImgs = JsonHelper.Deserialize<List<M_SeriesImages>>(strImagePath);
            State = "SUCCESS";
            bool verifyResult = verify();
            if (!verifyResult)
            {
                Response.Write(State);
                Response.End();
            }
            try
            {

                ProductSubmit();
                Response.Write("true");
            }
            catch
            {
                Response.Write("出现异常!");
            }
            Response.End();
        }

    }

    private static string ConvertJsonDateToDateString(Match m)
    {
        string result = string.Empty;
        result = DateTime.Now.ToString();
        return result;
    }

    private void ProductSubmit()
    {
        string seriesListImageBig = "";
        string seriesDetailImageBig = "";
        string seriesDetailImageList = "";
        string userManualImage = "";
        if (mImgs.Count > 3)
        {
            seriesListImageBig = mImgs[0].ImageUrl;
            seriesDetailImageBig = mImgs[1].ImageUrl;
            seriesDetailImageList = mImgs[2].ImageUrl;
            userManualImage = mImgs[3].ImageUrl;
        }
        int productId = ProductsService.CreateProduct(seriesId,
            name, model, bucketCapacity, workWeight, engine, power,
            applyTo, shortDescription, advantage, 0, "", seriesListImageBig
            , seriesDetailImageBig, seriesDetailImageList, userManualImage, DateTime.Now, 1, DateTime.Now, pdfUrl);
        ProductExtentionService.CreateProductExtention(productId, description, flashUrl, (short)1);
    }

    public bool verify()
    {
        bool verifyValue = true;
        if (seriesId == 0)
        {
            State = "请选择系列";
            verifyValue = false;
        }
        else
            if (string.IsNullOrEmpty(name.Trim()) || name.Length > 26)
            {
                State = "产品名称为空或超过指定长度26";
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(model) || model.Length > 20)
            {
                State = string.Format("挖机型号为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(bucketCapacity) || bucketCapacity.Length > 20)
            {
                State = string.Format("铲斗容量为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(workWeight) || workWeight.Length > 20)
            {
                State = string.Format("工作重量为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(engine) || engine.Length > 20)
            {
                State = string.Format("发动机机型为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(power) || power.Length > 20)
            {
                State = string.Format("净功率为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(applyTo) || applyTo.Length > 20)
            {
                State = string.Format("适用工矿为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(shortDescription) || shortDescription.Length > 1000)
            {
                State = string.Format("简短描述为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(advantage) || advantage.Length > 1000)
            {
                State = string.Format("优势描述为空或超长");
                verifyValue = false;
            }
            else if (string.IsNullOrEmpty(description) || description.Length > 1000)
            {
                State = string.Format("描述为空或超长");
                verifyValue = false;
            }
            else if (mImgs.Count > 10)
            {
                State = "上传图片数量超过5张";
                verifyValue = false;
            }
        //else if (!mUser.IsAuthencated)
        //{
        //    State = "用户未登录";
        //    verifyValue = false;
        //}
        return verifyValue;
    }



}